// ==UserScript==
// @name           DynaScriptRuntime
// @namespace      jirina42@seznam.cz
// @include        http*://orion.pardus.at/*
// @version        2.0
// ==/UserScript==

function GMreadCookie(name) {
    return localStorage.getItem(name);
}

function checkScriptAlreadyInstalled (head, src) {
    var scripts = head.getElementsByTagName ("script");
    for (var i=0; i<scripts.length; i++) {
        var aSrc = scripts[i].getAttribute("src");
        if (!aSrc) continue;
        if (aSrc.indexOf (src) != -1) return true;
    }
    return false;
}

function getServerUrl(file){
    var local = localStorage.getItem ('ds_local_dev');
    if (local == "true") {
	url = localStorage.getItem('ds_local_dev_path');
	if (url == null) var url = localStorage.getItem('ds_dynascript_address');
    } else {
	var url = localStorage.getItem('ds_dynascript_address');
    }
    if (url == null) {
        if (!localStorage.getItem ("po_invalid_url_shown")) {
            alert ("Dynascript server address is not set or it's invalid. Please ender valid server URL in the DynaScript control panel. You'll see this warning only once but none of the scripts requiring the server address will work until you enter a valid one.");
            localStorage.setItem ("po_invalid_url_shown", "yes");
        }
        return null;
    }
    if (file) {
        if (file[0] != "\/") url += "\/";
        url += file;
    }
    return url;
}

function getSuppressCaching () {
    return (localStorage.getItem ('ds_suppress_script_caching') == "true");
}

function loadScriptsForPage (universe, page) {
    // Loads the scripts applicable for the given page
    var scripts = GMreadCookie ("DS_installedScriptsConfig_"+universe);
    if (!scripts) return;
    
    // Decode the saved script elements and add them into the document's head to load them actually
    var div = document.createElement ("div");
    var head = document.getElementsByTagName('head')[0];
    div.innerHTML = scripts;
    var scripts = div.getElementsByTagName ("script");
    
    // Loop over the scripts and install them
    for (var i=0; i<scripts.length; i++) {
        aScript = scripts[i];
	var includes = aScript.getAttribute ("includes");
	
	// Skip scripts which do not have the "includes" attribute or the script should not be included into the current page
	if (!includes || includes.indexOf(page) == -1) continue;
	
	// Create a new <script> element
        var myScript = document.createElement ("script");
	myScript.type = "text/javascript";
	
	// Construct the script file's URL
        var url = aScript.getAttribute ("src");
        if (url.indexOf (":\/\/") == -1) {
            url = getServerUrl (url);
        }
	
	// Skip the script in case it's already installed
        if (checkScriptAlreadyInstalled (head, url)){
//GM_log ("Script already installed: "+url);
	    continue;
        }
        
	// Finally check if the script's target ir the page DOM and install the script if so
        if (aScript.parentNode.getAttribute ("target") == "page") {
            url += "?version="+aScript.parentNode.getAttribute ("version");
            if (getSuppressCaching()) url += "&time="+(new Date()).getTime ();
//GM_log ("Inserting script: "+aScript.parentNode.getAttribute ("scriptName") + " into "+page +". URL: "+url);
            myScript.setAttribute ("src", url);
	    head.appendChild (myScript);
        }
    }
}

function loadScriptsForCurrentPage () {
    // Load the scripts for the current page
    
    // Get the current URL code
    // So if the URL is e.g. http://orion.pardus.at/statistics.php?display=onlinelist
    // The result is simply statistics.php
    var url = document.URL.split ("//")[1].split("/")[1].split("?")[0];
    var uni = document.URL.split ("//")[1].split(".")[0];
    
    // Load the scripts
    loadScriptsForPage (uni, url);
}
loadScriptsForCurrentPage ();

